//
//  YJBBaseViewController.m
//  YunJiaBaoFirst
//
//  Created by 腾云小白龍 on 16/7/6.
//  Copyright © 2016年 腾云小白龍. All rights reserved.
//

#import "YJBBaseViewController.h"

@interface YJBBaseViewController ()
@property (strong, nonatomic) NSTimer * upDatatimer;           //刷新数据定时器
@end

@implementation YJBBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = DEFAULT_TEXT_COLOR;
    [self initUIBarButtonItemProperty];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark--//以颜色生成图片
- (UIImage*)ImageWithColor:(UIColor*)aColor{
    
    //* 以颜色创建图片
    CGSize imageSize = CGSizeMake(20, 20);
    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
    [aColor set];
    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return pressedColorImg;
    
}

#pragma mark-- 导航栏设置
-(void)setNavigationItemWithTitle:(NSString *)title{
    
    [self.navigationItem setTitle:title];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 10, 15)];
    [backBtn setImage:kImage(@"backBtn") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    //导航栏字体 和颜色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kColorWithRGB(188, 188, 187),NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};
    //导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavgationBar"] forBarMetrics:UIBarMetricsDefault];
    //    self.navigationController.navigationBar.tintColor = kColorWithRGB(188, 188, 187);
    
}

#pragma mark -- navigationBar 默认颜色
- (void)initUINavigationBarProperty {
    UINavigationBar * bar = [UINavigationBar appearance];
    NSMutableDictionary * barTextAttr = [NSMutableDictionary dictionary];
    barTextAttr[NSFontAttributeName] = [UIFont systemFontOfSize:18];
    barTextAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [bar setTitleTextAttributes:barTextAttr];
    [bar setBarTintColor:MAIN_COLOR];
    [bar setTintColor:[UIColor whiteColor]];
}


-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark -- 只设置标题
-(void)setNavigationTitle:(NSString*)title{
    
    [self.navigationItem setTitle:title];
    //导航栏字体 和颜色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kColorWithRGB(188, 188, 187),NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};
    //导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavgationBar"] forBarMetrics:UIBarMetricsDefault];

}



//开启定时器
-(void)startTimer:(int)time{
    // 如果定时器已开启，先停止再重新开启
    if (self.upDatatimer) [self stopTimer];
    
    self.upDatatimer = [NSTimer timerWithTimeInterval:time target:self selector:@selector(upDataWithTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.upDatatimer forMode:NSRunLoopCommonModes];
    
    
}

-(void)stopTimer{
    
    [self.upDatatimer invalidate];
    _upDatatimer = nil;
    
}

//撤销SVProgressHUD
-(void)SVProgressHUDDismissWithTime:(CGFloat)time{
    
    [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:time];
}

-(void)SVProgressHUDdismiss{
    
    [SVProgressHUD dismiss];
}

#pragma mark -- 更改tabbar button 的 字体颜色;
-(void)initUIBarButtonItemProperty{
    UITabBarItem *item = [UITabBarItem appearance];
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = TAB_BAR_ITEM_COLOR;
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    NSMutableDictionary *textAttrsSelect = [NSMutableDictionary dictionary];
    textAttrsSelect[NSForegroundColorAttributeName] = MAIN_COLOR;
    textAttrsSelect[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:textAttrsSelect forState:UIControlStateSelected];
    
}

@end
